package com.app.demo.controller;

import com.app.demo.bean.YourBean;
import com.app.demo.service.YourBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yourbean")
public class YourBeanController {
    @Autowired
    YourBeanService yourBeanService;
    @CrossOrigin
    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody YourBean yourBean){
        Map<String,Object> map = new HashMap<>();
        YourBean newYourBean = yourBeanService.login(yourBean.getName(),yourBean.getPassword());
        if(newYourBean!=null){
            map.put("success",true);
            map.put("msg","正确");
            map.put("imagePath",newYourBean.getImagePath());
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/regist")
    public Map<String,Object> regist(@RequestBody YourBean yourBean){
        Map<String,Object> map = new HashMap<>();
        YourBean newYourBean = yourBeanService.regist(yourBean);
        if(newYourBean!=null){
            map.put("success",true);
            map.put("msg","正确");
            map.put("imagePath",newYourBean.getImagePath());
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/list")
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<>();
        List<YourBean> yourBeanList = yourBeanService.findAll();
        if(yourBeanList!=null){
            map.put("success",true);
            map.put("msg","正确");
            map.put("list",yourBeanList);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody YourBean yourBean){
        Map<String,Object> map = new HashMap<>();
        YourBean newYourBean = yourBeanService.addNew(yourBean);
        if(newYourBean!=null){
            map.put("success",true);
            map.put("msg","正确");
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/delete/{id}")
    public Map<String,Object> delete(@PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        if(yourBeanService.findById(id) != null){//如果找到了就删除
            yourBeanService.deleteById(id);
            map.put("success",true);
            map.put("msg","正确");
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/findOne/{id}")
    public Map<String,Object> findOne(@PathVariable Integer id){
        Map<String,Object> map = new HashMap<>();
        YourBean yourBean =yourBeanService.findById(id);
        if(yourBean != null){//如果找到了就删除
            map.put("success",true);
            map.put("msg","正确");
            map.put("data",yourBean);
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
    @CrossOrigin
    @PostMapping("/update")
    public Map<String,Object> update(@RequestBody YourBean yourBean){
        Map<String,Object> map = new HashMap<>();
        YourBean newYourBean = yourBeanService.update(yourBean);
        if(newYourBean!=null){
            map.put("success",true);
            map.put("msg","正确");
        }else{
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}
